<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>商品管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="{{ url_for('static',filename='layuiadmin/layui/css/layui.css') }}" media="all">
    <link rel="stylesheet" href="{{ url_for('static',filename='layuiadmin/style/admin.css') }}" media="all">

</head>

<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-btn-group test-table-btn" style="margin-bottom: 10px;" id="data_bar"
                         lay-filter="data_bar">
{#                        <button type="button" class="layui-btn" lay-event="add">添加数据</button>#}
                    </div>

                    <table class="layui-hide" id="test-table" lay-filter="test-table"></table>

                    <script type="text/html" id="test-table-barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                    </script>
                </div>
            </div>
        </div>
    </div>

</div>

<!-- 展示图片大小 -->
<style type="text/css">
    .layui-table-cell {
        text-align: center;
        height: auto;
        white-space: normal;
    }

    .layui-table img {
        max-width: 100px
    }
</style>


<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv">
    <form class="layui-form " action="" lay-filter="dataFrm" id="dataFrm">

        <div class="layui-form-item">
            <div class="layui-col-md10">
                <label class="layui-form-label">开始时间：</label>
                <div class="layui-input-block">
                    <input type="text" name="start_time" placeholder="请输入开始时间" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md10">
                <label class="layui-form-label">结束时间：</label>
                <div class="layui-input-block">
                    <input type="text" name="end_time" placeholder="请输入结束时间" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
        </div>


        <input type="hidden" name="trade_id" id="trade_id">

        <div class="layui-form-item" style="margin-top:40px; ">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="doSubmit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>


    </form>
</div>
<!-- 添加和修改的弹出层结束 -->


<script src="{{ url_for('static',filename='layuiadmin/layui/layui.js') }}"></script>
<script>
    layui.config({
        base: '../../../static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'jquery', 'layer', 'form', 'upload'], function () {
        var admin = layui.admin;
        var table = layui.table;
        var upload = layui.upload;
        var $ = layui.jquery;
        // 参数d是当前行数据
        var statusTpl = function (d) {
            // if else判断
            if (d.trade_status == 0) {
                return '<input type="checkbox" lay-filter="switchGoodsID"  value="' + d.trade_id + '" lay-skin="switch" switch_goods_id="' + d.trade_id + '" lay-text="开启|关闭" checked> ';
            } else {
                return '<input type="checkbox" value="' + d.trade_id + '" lay-filter="switchGoodsID" switch_goods_id=""  lay-skin="switch" lay-text="开启|关闭">';
            }
        };
        var tableIns = table.render({
            elem: '#test-table',
            {#toolbar: "#data_bar",#}
            url: '/cms/trade_time_info',
            cols: [[
                {field: 'trade_id', title: 'ID', sort: true, align: 'center', hide: true,},
                {field: 'trade_name', title: '类型', sort: true, align: 'center'},
                {field: 'start_time', title: '开始时间', sort: true, align: 'center'},
                {field: 'end_time', title: '结束时间', sort: true, align: 'center'},
                {
                    field: 'trade_status', title: '状态', align: 'center', templet: statusTpl
                },
                {align: 'center', fixed: 'right', title: '操作', toolbar: '#test-table-barDemo',}
            ]]
            , page: true
        });


        // 添加数据按钮事件
        $('.test-table-btn .layui-btn').on('click', function () {
            openAdd()
        });


        //监听工具条
        table.on('tool(test-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('您确定要删除么?', function (index) {
                    $.ajax({
                        url: '/cms/mall/del_goods/',
                        type: "POST",
                        data: data,
                        success: function (data) {
                            if (data['code'] == 200) {
                                //关闭弹框
                                layer.close(mainIndex);
                                //显示提示框
                                layer.msg("成功", {icon: 6});
                                tableIns.reload()
                            } else {
                                layer.msg(data['message'], {icon: 5});
                            }
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                openUpdate(data);
            } else if (obj.event === 'jump') {
                var goods_id = data['goods_id'];
                window.location.href = "/mall/goods_detail_manage/?goods_id=" + goods_id
            }
        });


        var url;
        var mainIndex;

        //打开添加页面
        function openAdd() {
            mainIndex = layer.open({
                type: 1,
                title: '添加数据',
                content: $("#saveOrUpdateDiv"),
                {#area: ['800px', '650px'],#}
                area: ['60%', '80%'],
                success: function (index) {
                    //清空表单数据
                    $("#dataFrm")[0].reset();
                    // 清空id数据
                    document.getElementById("goods_id").value = "";
                    layui.form.render();
                }
            });
        }


        //打开修改页面
        function openUpdate(data) {
            mainIndex = layer.open({
                type: 1,
                title: '修改数据',
                content: $("#saveOrUpdateDiv"),
                {#area: ['800px', '650px'],#}
                area: ['600px', '450px'],
                success: function (index) {
                    layui.form.val("dataFrm", data);
                }
            });
        }

        //保存
        layui.form.on("submit(doSubmit)", function (obj) {
            //序列化表单数据
            var data = obj.field;
            console.log(data);
            $.ajax({
                url: '/cms/u_trade_time/',
                type: "POST",
                data: data,
                success: function (data) {
                    if (data['code'] == 200) {
                        //关闭弹框
                        layer.close(mainIndex);
                        //显示提示框
                        layer.msg("成功", {icon: 6});
                        tableIns.reload();
                        {#window.location.href = "{{ url_for('cms.cms_records') }}"#}
                    } else {
                        layer.msg(data['message'], {icon: 5});
                    }
                }
            });
            return false;

        });


        // 更改switch开关
        layui.form.on('switch(switchGoodsID)', function (data) {
            //开关是否开启，true或者false
            var checked = data.elem.checked;
            var is_type;
            var contexts;
            if (checked == true) {
                contexts = "开启";
                is_type = 0;
            } else {
                contexts = "关闭";
                is_type = 1;
            }
            //获取所需属性值
            var goods_id = data.value;
            console.log(is_type);
            console.log(goods_id);
            layer.confirm('您确定要' + contexts + '么?', function (index) {
                $.ajax({
                    url: '/cms/reset_trade/',
                    type: "POST",
                    data: {
                        'trade_id': goods_id,
                        'status': is_type,
                    },
                    success: function (data) {
                        if (data['code'] == 200) {
                            //关闭弹框
                            layer.close(mainIndex);
                            //显示提示框
                            layer.msg("成功", {icon: 6});
                            tableIns.reload();
                            {#window.location.href = "{{ url_for('cms.cms_records') }}"#}
                        } else {
                            layer.msg(data['message'], {icon: 5});
                        }
                    }
                });
                return false;
            });

        });

    });
</script>
</body>
</html>